package interceptor;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

import entities.TOperator;
import util.LogUtil;
import utils.StringUtil;

public class LiveInterceptor  implements Interceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2578561479301489061L;
	private String url;
	private Map<String,Object> map;
	private StringBuffer sb;
	
	public void destroy() {
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void init() {
		System.out.println("用户权限拦截器初始化");
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		String invocationAction=invocation.getAction().getClass().getName();		
		String invocationName=invocationAction.substring(invocationAction.lastIndexOf(".")+1,invocationAction.length());
		String invocationMethod=invocation.getProxy().getMethod();
		
		if(invocationName.toLowerCase().endsWith("action"))
		{
			if(invocation.getInvocationContext().getSession().get("operatorBean")==null) {
				return "timeout";
			}else{
				sb=new StringBuffer();
				map=invocation.getInvocationContext().getParameters();
				sb.append(((TOperator)invocation.getInvocationContext().getSession().get("operatorBean")).getOptId());
				sb.append(" "+invocationName+" "+invocationMethod+" ");
				for(Map.Entry<String, Object> entry: map.entrySet()){
					String[] vals=(String[]) entry.getValue();
					if(vals.length>0&&!StringUtil.isEmpty(vals[0])){
						sb.append("["+entry.getKey()+"("+vals[0]+")]");
					}
				}
				LogUtil.operate(sb.toString());
			}
		}
		return invocation.invoke();
	}

}
